using UnityEngine;
using System.Collections;

/*Comment
 * 士兵文件，士兵包含了个体的行为
 */

public enum ELeaderAction
{
	emAttack,
	emStopAttack,
	emMove,
	emStopMove,
	emBorn,
	emDeath,
	emRetreat,
}

public class LeaderMsg : System.EventArgs
{
	public ELeaderAction eAction;
	
	public LeaderMsg(ELeaderAction act)
	{
		eAction = act;
	}
} 

public class Soldier : MonoBehaviour {
	
	Animator animator;
	public GameObject rootObj;
	public event System.EventHandler<LeaderMsg> evtLeadEvent;

	// Use this for initialization
	void Start () {
		animator = GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () {

	}

	public void DoTurn(bool turn){

		AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo (0);
		if (turn) 
		{
			if(stateInfo.nameHash == Animator.StringToHash("Base Layer.Ready"))
			{
				animator.SetTrigger("t2Turn");
			}
		}
		else
		{
			if(stateInfo.nameHash == Animator.StringToHash("Base Layer.Turn"))
			{
				animator.SetTrigger("t2Ready");
			}
		}
	}
	
	public void DoMove(bool move){

		AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo (0);

		if (move) 
		{
			if(stateInfo.nameHash == Animator.StringToHash("Base Layer.Ready"))
			{
				animator.SetTrigger("t2Run");
			}
		}
		else
		{
			if(stateInfo.nameHash == Animator.StringToHash("Run.Runing") ||
			   stateInfo.nameHash == Animator.StringToHash("Run.RunStart"))
			{
				animator.SetTrigger("t2Ready");
			}
		}

		LeaderMsg evt;
		if (move) 
		{
			evt = new LeaderMsg(ELeaderAction.emMove);
		}
		else
		{
			evt = new LeaderMsg(ELeaderAction.emStopMove);
		}

		if (null != evtLeadEvent)
			evtLeadEvent (this, evt);
	}

	public void DoAttack(bool attack)
	{
		AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo (0);
		
		if (attack) 
		{
			// stop move
			if(stateInfo.nameHash == Animator.StringToHash("Run.Runing") ||
			   stateInfo.nameHash == Animator.StringToHash("Run.RunStart") ||
			   stateInfo.nameHash == Animator.StringToHash("Base Layer.Idle"))
			{
				animator.SetTrigger("t2Ready");
				animator.SetBool("bAttack", true);
			}

			// start attack
			//if we're in "Run" mode, respond to input for jump, and set the Jump parameter accordingly. 
			if(stateInfo.nameHash == Animator.StringToHash("Base Layer.Ready"))
			{
				animator.SetBool("bAttack", true);
			}
		}
		else
		{
			// stop move
			if(stateInfo.nameHash == Animator.StringToHash("Base Layer.Attack"))
			{
				animator.SetBool("bAttack", false);
			}
		}
	}

	public void AnimCallAttack()
	{
		LeaderMsg evt = new LeaderMsg (ELeaderAction.emAttack);

		evtLeadEvent (this, evt);
	}

	public void Retreat()
	{
		animator.SetTrigger("tDeath");
	}

	void CastSpell(){

	}
}
